package com.bicon.botu.mall.goods.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.entity.ProductRepertory;
import com.bicon.botu.mall.goods.vo.ProductRepertoryInfo;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.goods.service.IProductRepertoryService;
import com.bicon.botu.mall.goods.vo.ProductSkuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存中心
 *
 * @author SYG
 * @date 2017/7/25
 */
@CrossOrigin
@RestController
@RequestMapping("V1.0.0/productRepertory")
public class ProductRepertoryController extends BaseController {

    @Autowired
    private IProductRepertoryService productRepertoryService;

    /**
     * 查询库存信息
     *
     * @param category    分类path
     * @param productName 商品名称
     * @param serverName  店铺名称
     * @param pageSize    显示数量
     * @param pageNo      当前页码
     * @return
     */
    @RequestMapping("getRepertoryInfo")
    public BaseResult getRepertoryInfo(@RequestParam(value = "category", required = false) String category,
                                       @RequestParam(value = "productName", required = false) String productName,
                                       @RequestParam(value = "serverName", required = false) String serverName,
                                       @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) {
        Map<String, String> map = new HashMap();
        if (!StringUtils.isEmpty(category)) {
            map.put("category", "%" + category + "%");
        }
        if (!StringUtils.isEmpty(productName)) {
            map.put("productName", "%" + productName + "%");
        }
        if (!StringUtils.isEmpty(serverName)) {
            map.put("serverName", serverName);
        }
        Page page = productRepertoryService.getPage(new Page<ProductRepertoryInfo>(pageNo, pageSize), map);
        List<ProductRepertoryInfo> list = page.getRecords();
        List<ProductRepertoryInfo> newList = new ArrayList<>();
        for (ProductRepertoryInfo info : list) {
           List<ProductSkuVo> newListVo= productRepertoryService.getProductRepertorySku(info.getProductId());
            info.setProductSkuVo(newListVo);
            newList.add(info);
        }
        page.setRecords(newList);
        return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, page);
    }

    /**
     * 根据商品Id查询sku详情
     *
     * @param productId 商品Id
     * @return
     */
    @RequestMapping(value = "getProductSku", method = RequestMethod.POST)
    public BaseResult getProductSku(@RequestParam("productId") String productId) {
        return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, productRepertoryService.getProductRepertorySku(productId));


    }

    /**
     * 修改sku库存数量
     *
     * @param repertoryId 仓库id
     * @param surplusNum  库存数量
     * @return
     */
    @RequestMapping(value = "updateRepertoryNum", method = RequestMethod.POST)
    public BaseResult updateRepertoryNum(@RequestParam("repertoryId") String repertoryId, @RequestParam("surplusNum") Integer surplusNum) {
        ProductRepertory repertory = new ProductRepertory();
        repertory.setId(repertoryId);
        repertory.setProductNum(surplusNum);
        repertory.preUpdate();
        boolean result = repertory.updateById();
        if (result) {
            return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200);
        }
        return sendResult(GoodsConstants.RESPONSE_CODE_600, GoodsConstants.RESPONSE_MESSAGE_600);
    }

    /**
     * 根据仓库id删除商品
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public BaseResult deleteById(@RequestParam("id") String id, @RequestParam("productId") String productId) {
        Boolean result = new ProductRepertory().deleteById(id);
        List<ProductRepertory> list = new ArrayList<>();
        if (result) {
            list = new ProductRepertory().selectList(new EntityWrapper().eq("product_id", productId));
        }
        return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, list);
    }
}
